package com.pj.lib.auraServer.components.DB;

import java.sql.SQLException;

import com.pj.lib.auraServer.app.Server;
import com.pj.lib.auraServer.app.ServerComponent;
import com.pj.lib.auraServer.app.properties.DBProperties;
import com.pj.lib.auraServer.app.properties.Properties;
import com.pj.lib.auraServer.exceptions.AuraServerException;
import com.pj.lib.auraServer.exceptions.NoSuchDBException;

public class DBComponent extends ServerComponent {
	DB mDb;
	public DBComponent(Server server) throws ClassNotFoundException, SQLException, AuraServerException {
		super(server);
		
		if(getDBProperties().DB_TYPE.equals(DB.POSTGRESQL_DB)) {
			this.mDb = PostgresDB.getInstance(getDBProperties());
		} else {
			throw new AuraServerException("Aura cant handle DB called: " + getDBProperties().DB_TYPE);
		}

		if(!this.mDb.isConnected()) {

			this.mDb.connect();
		}
		
		
		this.mDb.createDB();
	}
	
	public DB getDB() {
		return mDb;
	}
	
	private DBProperties getDBProperties() {
		return (DBProperties)getServer().getProperties(Properties.PROP_DB);
	}
}
